package hui.zhang.web.action.take_delivery;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import hui.zhang.commons.CommonUtils;
import hui.zhang.domain.take_delivery.Promotion;
import hui.zhang.service.take_delivery.PromotionService;
import hui.zhang.utils.BaseAction;

/**
 * 促销
 * 
 * @author hui.zhang
 * @date 2017年12月20日 下午8:25:39
 */
@ParentPackage("struts-default")
@Namespace("/")
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class PromotionAction extends BaseAction<Promotion> {

	@Autowired
	private PromotionService promotionService;
	
	private File titleImgFile;
	private String titleImgFileFileName;

	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}

	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}

	@Action(value = "promotion_save", results = {
			@Result(name = "success", location = "/pages/take_delivery/promotion.html", type = "redirect") })
	public String save() throws IOException {
		// 宣传图 上传、在数据表保存 宣传图路径
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");
		String saveUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";
		// 生成随机图片名
		String randomFileName = CommonUtils.uuid() + "_" + titleImgFileFileName;
		// 保存图片 (绝对路径)
		File destFile = new File(savePath + "/" + randomFileName);
		System.out.println(destFile.getAbsolutePath());
		FileUtils.copyFile(titleImgFile, destFile);

		// 将保存路径 相对工程web访问路径，保存model中
		model.setTitleImg(ServletActionContext.getRequest().getContextPath() + "/upload/" + randomFileName);

		// 调用业务层，完成活动任务数据保存
		promotionService.save(model);
		return SUCCESS;
	}
	
	/**
	 * 分页显示活动列表
	 * @return
	 * @throws IOException 
	 */
	@Action(value="promotion_pageQuery")
	public String pageQuery() throws IOException {
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Promotion> pageData =  promotionService.findByPage(pageable);
		List<Promotion> rows = pageData.getContent();
		long total = pageData.getTotalElements();
		// 修改全局的全局日期格式  
		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", rows);
		String jsonString = JSON.toJSONString(map, SerializerFeature.WriteDateUseDateFormat);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(jsonString);
		return NONE;
	}

}
